<template>
  <!-- <view class="statusBar"></view> -->
  <view class="container">
    <view class="header">
      <image src="../../static/icon.png" class="logo"></image>
      <text class="title">欢迎来到温州理工学院</text>
    </view>
    <view class="form">
      <uni-forms ref="loginFormRef" :model="loginForm" :rules="loginRules" validateTrigger="blur" labelWidth="80px">
        <uni-forms-item label="学号/工号" name="number">
          <uni-easyinput type="digit" v-model="loginForm.number" placeholder="请输入学号/工号"></uni-easyinput>
        </uni-forms-item>
        <uni-forms-item label="密码" name="password">
          <uni-easyinput type="password" v-model="loginForm.password" placeholder="请输入密码"></uni-easyinput>
        </uni-forms-item>
      </uni-forms>
      <view class="operation">
        <text class="btnText" @click="forgetHandler">忘记密码</text>
        <!-- <text class="btnText" :class="disabledClass" @click="univerifyLogin">一键登录</text> -->
      </view>
      <button :disabled="!useGlobalStore.agreePolicy" type="primary" @click="submitHandler">
        {{ !useGlobalStore.agreePolicy ? '请先阅读并同意协议' : '登录' }}
      </button>
    </view>
    <view class="policyContainer">
      <checkbox class="policy" :checked="useGlobalStore.agreePolicy" @click="agreeHandler">
        <text>已阅读并同意</text>
      </checkbox>
      <uni-link>服务协议</uni-link>、<uni-link>隐私协议</uni-link>
    </view>
  </view>
  <Loading />
</template>

<script>
  import { userStore } from '@/store/user'

  export default {
    onLoad() {
      const useUserStore = userStore()
      if (useUserStore.token)
        httpRequest({
          method: 'GET',
          url: '/verify'
        }).then((res) => {
          if (res.data.logged) {
            uni.switchTab({
              url: '/pages/index/index'
            })
          }
        })
    }
  }
</script>

<script setup>
  import { onBeforeMount, reactive, ref, watch } from 'vue'
  import httpRequest from '@/utils/request.js'
  import { userStore } from '@/store/user'
  import { globalStore } from '@/store/global'
  import Loading from '@/components/Loading.vue'

  const useUserStore = userStore()
  const useGlobalStore = globalStore()
  const loginFormRef = ref()
  const loginForm = reactive({
    number: '',
    password: ''
  })
  const loginRules = {
    number: {
      rules: [
        {
          required: true,
          errorMessage: '请输入学号/工号'
        }
      ]
    },
    password: {
      rules: [
        {
          required: true,
          errorMessage: '请输入密码'
        }
      ]
    }
  }

  function submitHandler() {
    // console.log(this.$refs.loginFormRef);
    loginFormRef.value.validate().then((res) => {
      httpRequest({
        url: '/login',
        method: 'POST',
        data: res
      })
        .then((res) => {
          useUserStore.setToken(res.data.token)
          uni.switchTab({
            url: '/pages/index/index'
          })
        })
        .catch((err) => {
          console.log(err)
        })
    })
  }

  function forgetHandler() {
    uni.navigateTo({
      url: '/pages/login/forgetPassword/forgetPassword'
    })
  }

  const agreeHandler = () => {
    useGlobalStore.setAgreePolicy(!useGlobalStore.agreePolicy)
  }

  onBeforeMount(() => {
    uni.hideTabBar()
  })
</script>

<style scoped lang="scss">
  .container {
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100vh;
    // border: 1px solid;
    width: 100%;
    // padding: var(--status-bar-height) 30rpx 0 30rpx;
    padding: 0 30rpx;
    overflow: scroll;

    .header {
      height: 40%;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;

      .logo {
        height: 120rpx;
        width: 120rpx;
        margin-bottom: 40rpx;
      }

      .title {
        font-size: 40rpx;
        font-weight: bold;
      }
    }

    .header::before {
      z-index: -1;
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 40%;
      background: linear-gradient(to bottom, rgba(24, 71, 149, 0.6), transparent);
    }

    .form {
      // height: 100%;
      width: 100%;

      .operation {
        margin-bottom: 20rpx;
        display: flex;
        justify-content: space-between;

        .btnText {
          font-size: 24rpx;
          color: $my-wzut-icon-primary;
        }

        .btnText:active {
          opacity: 0.3;
        }
      }

      .codeInput {
      }

      .codeImg {
        width: 200rpx;
        height: 80rpx;
      }
    }

    .policyContainer {
      font-size: 24rpx;
      margin-top: 20rpx;
      display: flex;
      align-items: center;
    }
  }

  .statusBar {
    padding-top: var(--status-bar-height);
    position: fixed;
    height: var(--status-bar-height);
    background-color: white;
    z-index: 999;
    width: 100%;
  }

  .disabled {
    color: darkgray !important;
    pointer-events: none;
  }
</style>
